/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sk.ke.szatmary.peter.jclac.simpleCiphers;

import sk.ke.szatmary.peter.jclac.simpleCiphers.core.SimpleCipher;
import sk.ke.szatmary.peter.jclac.simpleCiphers.core.AbstractCipher;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Vernam
 * 
 * first attribute (0): String key
 * 
 * @author nue
 */
public class Vernam extends AbstractCipher implements SimpleCipher{
    

    private Vernam(List<SimpleCipherAttr> attrs) {
        this.attrs = attrs;
    }

    public Vernam() {
        attrs = new ArrayList<SimpleCipherAttr>();
    }
    

    /**
     * key must be random and with the same length like a plain text,
     * so must be generated because the key can be compute at the time
     * when is plain text known, and this is by calling method String getEncrypt(String),
     * so thats the reason why:
     * <ul>
     * <li>one parameter constructor is not public </li>
     * <li>void setAttribute(List list) method is not implemented, just you can read the key
     * with List<SimpleCipherAttr> getAtrributes(), setting new key is done with calling 
     * method String getEncrypted(String decrypted), logic is right because this cipher with key to one usage</li>
     * </ul>
     * @param plain
     * @return 
     */
    private String generateKey(String plain) {
        Random random = new Random();
        char randomChar;
        StringBuilder str = new StringBuilder();
        for(int i=0;i<plain.length();i++) {
            randomChar = (char) random.nextInt(END_UNICODE);
            str.append(randomChar);
        }
        attrs.add(new SimpleCipherAttr(str.toString()));
        return str.toString();
    }
    
    
    @Override
    public String getCipherName() {
        return super.getName();
    }


    @Override
    public List<SimpleCipherAttr> getAttributes() {
        return super.getAtrributes();
    }

    @Override
    public void setAttributes(List<SimpleCipherAttr> list) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public String getEncrypted(String plain) {
        String key = this.generateKey(plain);
        this.attrs.add(new SimpleCipherAttr(key));
        return null;
    }

    @Override
    public String getDecrypted(String encrypted) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public String toString() {
        return this.getName() + "{" + "key=" + this.attrs.get(0).getValue() + '}';
    }
    
    
    
    
}
